package com.ghn.cocknovel.view

import android.content.Context
import android.os.Looper
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.ghn.cocknovel.R
import com.tencent.navix.api.layer.NavigatorLayer
import com.tencent.navix.api.layer.NavigatorLayerRoot.NavModeChangeCallback
import com.tencent.navix.api.layer.NavigatorLayerRootDrive
import com.tencent.navix.api.model.NavDriveDataInfo
import com.tencent.navix.api.model.NavLaneInfo
import com.tencent.navix.api.model.NavMode
import com.tencent.navix.api.navigator.NavigatorDrive
import com.tencent.navix.api.observer.SimpleNavigatorDriveObserver
import com.tencent.navix.ui.internal.NavPreviewButtonView
import java.util.Locale

/**
 * @author 浩楠
 *
 * @date 2023/10/17-22:20.
 *
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * @Description: TODO 导航样式
 */
class CustomNavView : RelativeLayout, NavigatorLayer<NavigatorDrive?> {
    private var navigatorDrive: NavigatorDrive? = null
    private var turnIcon: ImageView? = null
    private var distanceInfo: TextView? = null
    private var guideLane: ImageView? = null
    private var navDriveDataInfo: NavDriveDataInfo? = null
    private var previewButtonView: NavPreviewButtonView? = null
    private var layerRootDrive: NavigatorLayerRootDrive? = null

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.layout_custom_view, this)
        turnIcon = findViewById<ImageView>(R.id.turn_icon)
        distanceInfo = findViewById<TextView>(R.id.distance_info)
        guideLane = findViewById<ImageView>(R.id.guide_lane)
        previewButtonView = findViewById<NavPreviewButtonView>(R.id.preview_button)
        visibility = GONE
        previewButtonView?.setOnClickListener(OnClickListener {
            if (layerRootDrive != null) {
                layerRootDrive!!.navMode =
                    if (layerRootDrive!!.navMode == NavMode.MODE_OVERVIEW) NavMode.MODE_3D_TOWARDS_UP else NavMode.MODE_OVERVIEW
            }
        })
    }

    fun bindLayerRoot(layerRootDrive: NavigatorLayerRootDrive?) {
        this.layerRootDrive = layerRootDrive
        this.layerRootDrive!!.addNavModeChangeCallback(navModeChangeCallback)
    }

    fun unbindLayerRoot() {
        if (layerRootDrive != null) {
            layerRootDrive!!.removeNavModeChangeCallback(navModeChangeCallback)
        }
        layerRootDrive = null
    }

    override fun getView(): View {
        return this
    }

    override fun onViewBound(navigatorDrive: NavigatorDrive?) {
        this.navigatorDrive = navigatorDrive
        this.navigatorDrive!!.registerObserver(navigatorDriveObserver, Looper.getMainLooper())
    }

    override fun onViewUnBound() {
        navigatorDrive!!.unregisterObserver(navigatorDriveObserver)
        navigatorDrive = null
    }

    var navModeChangeCallback =
        NavModeChangeCallback { navMode, b -> previewButtonView!!.onNavModeChange(navMode, b) }
    var navigatorDriveObserver: SimpleNavigatorDriveObserver =
        object : SimpleNavigatorDriveObserver() {
            override fun onNavDataInfoUpdate(navDriveDataInfo: NavDriveDataInfo) {
                super.onNavDataInfoUpdate(navDriveDataInfo)
                if (this@CustomNavView.navDriveDataInfo == null) {
                    visibility = VISIBLE
                }
                this@CustomNavView.navDriveDataInfo = navDriveDataInfo
                val mainRouteData = navDriveDataInfo.getMainRouteData()
                if (mainRouteData != null) {
                    turnIcon!!.setImageBitmap(mainRouteData.nextIntersectionBitmap)
                    val text: String
                    val distance = mainRouteData.getNextIntersectionRemainingDistance()
                    text = if (distance > 1000) {
                        String.format(Locale.getDefault(), "%.1f公里", distance / 1000f)
                    } else if (distance <= 0) {
                        "现在"
                    } else {
                        String.format(Locale.getDefault(), "%d米", distance)
                    }
                    distanceInfo!!.text = text
                }
            }

            override fun onWillShowLaneGuide(laneInfo: NavLaneInfo) {
                super.onWillShowLaneGuide(laneInfo)
                guideLane!!.setImageBitmap(laneInfo.getGuideLaneBitmap())
            }

            override fun onHideLaneGuide() {
                super.onHideLaneGuide()
                guideLane!!.setImageBitmap(null)
            }
        }
}